﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ajeelee.Core.Utils;
using BoCaiWeb.Business;
using BoCaiWeb.Model;
using BoCaiWeb.WebSite.Code;

namespace BoCaiWeb.WebSite.WebAdmin
{
    public partial class ArticleEdit : AuthPageBase
    {
        protected int id;

        protected News newsInfo = new News();
        protected IList<NewsClass> newsClassList = new List<NewsClass>();

        INewsBusiness newsBll = new NewsBusinessFactory().CreateNewsBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            id = QueryParamInt32("id");
            try
            {
                newsClassList = Utility.GetNewsClass().Where(c => c.PId == 1).ToList();

                if (id > 0)
                {
                    newsInfo = newsBll.GetById(id);
                    if (newsInfo == null)
                    {
                        newsInfo = new News();
                        MessageBoxGo("不存在该信息", FormParam("sourceurl", "ArticleList.aspx"));
                    }
                }
                if (IsPost)
                {
                    if (id > 0)   // 更新
                    {
                        initModel();
                        newsBll.Update(newsInfo);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo(FormParam("sourceurl", "ArticleList.aspx"), true);

                    }
                    else          // 新建
                    {
                        initModel();
                        newsBll.Insert(newsInfo);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo(FormParam("sourceurl", "ArticleList.aspx"), true);
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }

        public void initModel()
        {
            // 就初始首页xml
            if (id <= 0)
            {
                newsInfo = new News();
                newsInfo.Author = User.Identity.Name;
                newsInfo.InTime = DateTime.Now;
                newsInfo.InIP = IPAddress;
                newsInfo.State = 2;
            }
            newsInfo.Title = FormParam("title");
            newsInfo.Keyword = FormParam("keyWord");
            newsInfo.CId = FormParamInt32("cateid");
            newsInfo.Pic = FormParam("faceImg");
            newsInfo.Syno = FormParam("summary");
            newsInfo.Content = FormParam("content");
            newsInfo.Top = FormParam("top");
            newsInfo.Source = FormParam("source");

            newsInfo.UpTime = DateTime.Now;
            newsInfo.UpIP = IPAddress;
        }
    }
}